package com.tanhua.server.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.model.domain.Question;
import com.tanhua.model.domain.Settings;
import com.tanhua.model.domain.User;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.ErrorResult;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.server.exception.BusinessException;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.service.BlackListApi;
import com.tanhua.service.QuestionApi;
import com.tanhua.service.SettingsApi;
import com.tanhua.service.UserApi;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author げっしょく
 * @date 2021/10/24
 */
@Service
public class UserSettingService {

    @DubboReference
    private QuestionApi questionApi;

    @DubboReference
    private SettingsApi settingsApi;

    @DubboReference
    private BlackListApi blackListApi;

    @DubboReference
    private UserApi userApi;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    private String Up = "UPDATE-PHONE";

    public SettingsVo getSettings() {
        //获取用户的手机号和ID
        Long id = UserHolder.getId();
        User user = userApi.getByID(id);
        String phone = user.getMobile();

        //获取用户的陌生人问题，没有设置的话设置一个默认信息
        Question question = questionApi.getById(id);
        if (question == null) {
            question = new Question();
            question.setUserId(id);
            question.setTxt("你也想长寿吗？还是喜欢心情愉悦");
        }

        //查询用户的通知设置
        Settings settings = settingsApi.getByUserId(id);

        //设置用户的手机、id、陌生人问题、通知设置
        SettingsVo settingsVo = new SettingsVo();
        settingsVo.setId(id);
        settingsVo.setPhone(phone);
        settingsVo.setStrangerQuestion(question.getTxt());
        if (settings != null) {
            settingsVo.setGonggaoNotification(settings.getGonggaoNotification());
            settingsVo.setLikeNotification(settings.getLikeNotification());
            settingsVo.setPinglunNotification(settings.getPinglunNotification());
        }
        return settingsVo;

    }

    /**
     * 设置或更新用户的陌生人问题
     * @param content 用户设置的陌生人问题
     */
    public void setquestions(String content) {
        Question question = questionApi.getById(UserHolder.getId());
        if (question == null) {
            question = new Question();
            question.setUserId(UserHolder.getId());
            question.setTxt(content);
            questionApi.save(question);
        }else {
            question = new Question();
            question.setUserId(UserHolder.getId());
            question.setTxt(content);
            questionApi.update(question);
        }
    }

    /**
     * 修改通知设置
     * @param settings 用户需要修改的通知设置
     */
    public void setSettings(Settings settings) {
        settings.setUserId(UserHolder.getId());
        Settings settings1 = settingsApi.getByUserId(UserHolder.getId());
        if (settings1 == null) {
            settingsApi.save(settings);
        }else {
            settingsApi.update(settings);
        }
    }

    /**
     * 分页查询黑名单用户
     * @param page 页码
     * @param pagesize 每页条数
     * @return 返回的黑名单用户列表
     */
    public PageResult blacklist(Integer page, Integer pagesize) {
        Page page1 = new Page<>(page, pagesize);

        IPage<UserInfo> p = blackListApi.getPageUser(UserHolder.getId(), page1);
        PageResult pageResult = new PageResult(page, pagesize, p.getTotal(), p.getRecords());

        return pageResult;

    }

    /**
     * 删除黑名单用户
     * @param blaceUserId 需要移除的黑名单用户Id
     */
    public void delectBlack(Long blaceUserId) {
        Long userId = UserHolder.getId();
        blackListApi.delectBlack(userId, blaceUserId);

    }


    /**
     * 修改手机号：发送验证码
     */
    public void updatephoneSendVerCode() {
        String phone = UserHolder.getPhone();
        //验证码获取
        //String code = RandomStringUtils.randomNumeric(6);
        String code = "111111";
        //发送验证码
        //sendSms.sendSms(phone, code);
        //将验证码存入redis
        redisTemplate.opsForValue().set(Up + phone, code, 5, TimeUnit.MINUTES);
    }

    /**
     * 比较验证码
     * @param code 用户输入的味道
     * @return
     */
    public Map checkVerificationCode(String code) {
        String s = redisTemplate.opsForValue().get(Up + UserHolder.getPhone());
        if (code == null || !s.equals(code)) {
            throw new BusinessException(ErrorResult.loginError());
        }

        Map<String, Boolean> map = new HashMap<>();
        map.put("verification", true);
        return map;
    }

    /**
     * 修改手机号码：填入需要修改的手机号码
     * @param phone 需要修改成的手机号码
     */
    public void updatephone(String phone) {
        User user = new User();
        user.setId(UserHolder.getId());
        user.setMobile(phone);
        userApi.updateById(user);
    }


}
